package com.hbean.service.impl;

import com.hbean.entity.SysMenu;
import com.hbean.mapper.SysMenuMapper;
import com.hbean.service.SysMenuService;
import com.hbean.system.config.SysCache;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-28 15:42
 * @ModificationHistory:
 */

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysCache sysCache;

    @Override
    @CachePut(value = "sysCache", key="'sysMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysMenu> insertDataAndUpdateCache(SysMenu sysMenu) {
        
        sysMenu.setSm03(sysMenu.getSm02());//父级节点编号
        sysMenu.setSm04(sysCache.getSysMenuNameByID(sysMenu.getSm02()));
        sysMenu.setSm00(HUtil.uuidOf18());//主键
        sysMenu.setSm02(HUtil.uuidOf18());//唯一标识

        this.sysMenuMapper.insertData(sysMenu);

        return this.sysMenuMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysMenu> deleteDataAndUpdateCache(SysMenu sysMenu) {
        
        this.sysMenuMapper.deleteData(sysMenu);

        return this.sysMenuMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysMenu> updateDataAndUpdateCache(SysMenu sysMenu) {

        this.sysMenuMapper.updateData(sysMenu);

        return this.sysMenuMapper.selectDatas(new Condition());
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysMenu'") //开启缓存策略
    public List<SysMenu> getDataForCache() {
        return this.sysMenuMapper.selectDatas(new Condition());
    }
}
